/*
 *  $TEAM_NAME Space Trader
 */
package presenters;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Observable;
import java.util.Observer;

import javax.swing.JButton;

import models.Approach;
import models.Encounter;
import models.Encounter.Pirate;
import views.EncounterPanel;
import views.HubPanel;

/**
 * Encounter Presenter
 * @author twb
 * @version 1.0
 */
public class EncounterPresenter extends Presenter<EncounterPanel> 
implements ActionListener, Observer {
	/**
	 * field
	 */
	private Approach approach;

	/**
	 * field
	 */
	private Encounter encounter;
	
	/**
	 * new encounter presenter
	 * @param approach
	 * @param view
	 */
	public EncounterPresenter(Approach approach, EncounterPanel view){
		this.approach = approach;
		
		view.addActionListener(this);
		super.setView(view);
		
		this.approach.addObserver(this);
	}
	
	/**
	 * @return the approach
	 */
	public Approach getApproach() {
		return approach;
	}

	/**
	 * @param approach the approach to set
	 */
	public void setApproach(Approach approach) {
		this.approach = approach;
	}

	/**
	 * action performed
	 * @param e
	 */
	@Override
	public void actionPerformed(ActionEvent e) {
		final Object source = e.getSource();

        if (source instanceof JButton) {
            final JButton button = (JButton) source;
            
            if(button.getText().equals("Next Turn")){
            	this.approach.nextTurn();
            	if(this.approach.getProgress().getValue() 
            			>= this.approach.getProgress().getMaximum()) {
            		final HubPresenter hub = new HubPresenter(approach.getGame(), 
            				new HubPanel(approach.getGame(), this.getFrame()));
                    present(hub);
            	} else if(Encounter.isOccuring()){
            		this.encounter = new Encounter(Encounter.Type.PIRATE, 
            				this.approach.getGame().getPlayer());
            		this.encounter.addObserver(this);
            		this.view.setEncounter(encounter);
            	}
            	this.view.update();
            } else if(button.getText().equals("Attack")){
            	final Pirate enemy = (Pirate) this.encounter.getCharacter();
            	this.view.getEncounter().getPlayer().defend((enemy).getShip().attack());
            	enemy.getShip().defend(encounter.getPlayer().attack());
            	
            	if(enemy.getShip().getHealth().getValue() <= 0) {
            		this.view.setEncounter(null);
            		this.view.update();
            	}
            } else if(button.getText().equals("Flee")){
            	this.approach.nextTurn();
            	this.view.getEncounter().getPlayer().defend(
            			((Pirate) this.encounter.getCharacter()).getShip().attack());
            	this.view.update();
            }
        }
		
	}
	
	/**
	 * update
	 * @param obs
	 * @param arg
	 */
	@Override
	public void update(Observable obs, Object arg) {
		System.out.println("something happened");
	}

	/**
	 * to string
	 * @return string
	 */
	@Override
	public String toString() {
		return "";
	}
}
